package com.geely.jdemo.wildchar;

import org.junit.Test;

/**
 * 7.通配符类型
 */
public class WildChar {
    public static void print(GenericType<Fruit> genericType) {
        System.out.println(genericType.getData().getColor());
    }

    public static void printExtends(GenericType<? extends Fruit> genericType) {
        System.out.println(genericType.getData().getColor());
    }

    public static void printSuper(GenericType<? super Apple> genericType) {
        System.out.println(genericType.getData());
    }

    @Test
    public void mainTest() {
        GenericType<Fruit> typeFruit = new GenericType<>();
        print(typeFruit);
        GenericType<Orange> typeOrange = new GenericType<>();
//        print(typeOrange);//类型不匹配

        printExtends(typeOrange);//使用通配符？之后
//        print2(new GenericType<Food>());//extends Fruit类型的上界
        GenericType<? extends Fruit> g = typeOrange;

        Apple apple = new Apple();
        Fruit fruit = new Fruit();
        //extends 只可取 不可存
//        g.setData(apple);
//        g.setData(fruit);
        Fruit data = g.getData();

        //supper 只可存 不可取
        GenericType<? super Apple> appleGeneric = new GenericType<>();
//        Apple data1 = appleGeneric.getData();//只能是Object
        appleGeneric.setData(apple);
        appleGeneric.setData(new HongFuShi());
//        appleGeneric.setData(fruit);

        GenericType<Fruit> fruitGenericType = new GenericType<>();
        GenericType<Apple> appleGenericType = new GenericType<>();
        GenericType<Orange> orangeGenericType = new GenericType<>();
        GenericType<HongFuShi> hongFuShiGenericType = new GenericType<>();

        printSuper(fruitGenericType);
        printSuper(appleGenericType);
//        printSuper(orangeGenericType);//平级 毫无关系
//        printSuper(hongFuShiGenericType);//子类不符合supper
    }
}

class Food {
}

class Fruit extends Food {
    private String color;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

class Apple extends Fruit {
}

class Orange extends Fruit {
}

class HongFuShi extends Apple {
}